package com.cqc.timecountdown;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final int MSGWHAT1 = 1;
    private static final int MSGWHAT2 = 2;
    private int CountDown = 5;
    private Button btn;
    private Button btn2;
    private Button btn3;
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViews();
        initViews();
    }

    private void initViews() {
        btn.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
    }

    private void findViews() {
        btn = (Button) findViewById(R.id.btn);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);
        tv = (TextView) findViewById(R.id.tv);
    }

    //怎么防止重复点击
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn:
                handler.sendEmptyMessage(MSGWHAT1);
                break;
            case R.id.btn2:
                handler.sendEmptyMessage(MSGWHAT2);
                break;
            case R.id.btn3://内存泄漏，
                Timer timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (CountDown >= 0) {
                                    btn3.setEnabled(false);
                                    tv.setText(CountDown-- + "s");
                                }else{
                                    btn3.setEnabled(true);
//                                    CountDown = 5;//死循环
                                }
                            }
                        });
                    }
                }, 1000, 1000);//1秒后，每隔1秒改变一次
                break;
        }
    }


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            if (msg.what == MSGWHAT1) {//倒计时实现倒计时效果
                if (CountDown >= 0) {
                    btn.setEnabled(false);//防止重复点击Button，否则多次点击button：每隔一秒发送消息多次执行
                    tv.setText(CountDown-- + "s");
                    handler.sendEmptyMessageDelayed(MSGWHAT1, 1000);
                } else {
                    btn.setEnabled(true);
                    CountDown = 5;//复位，5s倒计时完后可以继续倒计时
                    tv.setText(CountDown-- + "s");
                }
            } else if (msg.what == MSGWHAT2) {//Handler + Thread 实现倒计时效果
                if (CountDown >= 0) {
                    btn2.setEnabled(false);
                    tv.setText(CountDown-- + "s");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        handler.sendEmptyMessage(MSGWHAT2);
                    }
                } else {
                    btn2.setEnabled(true);
                    CountDown = 5;
                    tv.setText(CountDown-- + "s");
                }
            }
        }
    };

    @Override
    protected void onStop() {
        super.onStop();
        handler.removeMessages(MSGWHAT1);
        handler.removeMessages(MSGWHAT2);
    }
}
